/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.business.impl;

import com.alibaba.druid.util.StringUtils;
import com.aliyun.oss.OSSClient;
import com.bm.console.common.OSSConfig;
import com.sys.object.common.page.PageBean;

import com.bm.console.business.WebNewsTypeService;
import com.bm.console.pojo.Tree;
import com.bm.console.pojo.WebNewsType;
import com.bm.console.pojo.mapper.WebNewsTypeMapper;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sys.object.common.utils.Base64Helper;
import com.sys.object.common.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.sys.object.common.page.PageBean;

@Service
public class WebNewsTypeServiceImpl implements WebNewsTypeService {

    @Autowired
    WebNewsTypeMapper webNewsTypeMapper;

    @Override
    public PageBean queryPageList(WebNewsType entity, PageBean page) throws Exception {
        int rowCount = webNewsTypeMapper.selectByPageCount(entity);
        page.setRowCount(rowCount);
        entity.setOffset(page.getStartRow());
        entity.setLimit(page.getPerpage());
        page.setList(webNewsTypeMapper.selectByPage(entity));
        return page;
    }

    public WebNewsType queryByPrimaryKey(Integer id) throws Exception {
        return webNewsTypeMapper.selectByPrimaryKey(id);
    }

    public int saveOrUpdate(WebNewsType entity) throws Exception {
        if (!StringUtils.isEmpty(entity.getFiledata()) && !StringUtils.isEmpty(entity.getFileext())) {
            byte[] data = Base64Helper.getImageFromBASE64ToByte(entity.getFiledata());
            String filename = UUID.randomUUID() + "." + entity.getFileext();

            OSSClient ossClient = new OSSClient(OSSConfig.oss_endpoint, OSSConfig.oss_accessKeyId,
                    OSSConfig.oss_accessKeySecret);

            String eTag = OSSUtil.uploadObject2OSS(ossClient, data, filename, OSSConfig.oss_backetName, "news/");
            ossClient.shutdown();
            if (!StringUtils.isEmpty(eTag)) {
                entity.setPhoto(OSSConfig.oss_domain + "news/" + filename);
            }
        }
        if (entity.getId() != null && entity.getId() > 0) {
            entity.setUpdateTime(new Date());
            return this.webNewsTypeMapper.updateByPrimaryKeySelective(entity);
        }
        entity.setCreateTime(new Date());
        return this.webNewsTypeMapper.insert(entity);
    }

    public int remove(Integer id) throws Exception {
        return this.webNewsTypeMapper.deleteByPrimaryKey(id);
    }

    public int delete(Integer id) throws Exception {
        return this.webNewsTypeMapper.deleteByPrimaryKey(id);
    }

    public int updateStatus(WebNewsType entity) throws Exception {
        return this.webNewsTypeMapper.updateByPrimaryKeySelective(entity);
    }

    public int updateStatus(String status, String[] ids)
            throws Exception {

        return 0;
    }

    @Override
    public List<Tree> selectAll(WebNewsType record) throws Exception {
        return this.webNewsTypeMapper.selectAll(record);
    }


}
